package com.mindainfo.configCenter.config.web;

import com.mindainfo.configCenter.base.common.Response;
import com.mindainfo.configCenter.base.common.ResponseFactory;
import com.mindainfo.configCenter.base.dao.impl.MorphiaDao;
import com.mindainfo.configCenter.config.bean.Emoji;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by MD-01 on 2017/1/5.
 */
@RestController
@RequestMapping("/emoji")
public class EmojiController {

    @Resource
    private MorphiaDao morphiaDao;

    @RequestMapping("/add")
    public Response<?> add(String name){

        Emoji emoji = new Emoji();
        emoji.setCreateTime(new Date());
        emoji.setName(name);
        morphiaDao.getDatastore().save(emoji);

        List<Emoji> emojiList = morphiaDao.getDatastore().createQuery(Emoji.class)
                .asList();

        List<String> names = new ArrayList<>();
        for(Emoji e : emojiList){
            names.add(e.getName());
        }
        return ResponseFactory.ok(names);
    }
}
